﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using QuanLyBanHang.QuerySQL;

namespace QuanLyBanHang
{
    public partial class frSanpham : Office2007Form
    {
        bool ts,ktra;
        ClassSanpham sp = new ClassSanpham();
        public frSanpham()
        {
            InitializeComponent();
        }
        public void Xulybutton(bool b)
        {
            this.btnsua.Enabled = btnthem.Enabled = b;
            btnLuu.Enabled = btnhuy.Enabled = panel_txt.Enabled = !b;
        }
        private void frSanpham_Load(object sender, EventArgs e)
        {
            Xulybutton(true);
            dtgv_Sanpham.DataSource = sp.ShowSP();
        }

        private void dtgv_Sanpham_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
        {
            try
            {
                for (int i = 0; i < dtgv_Sanpham.RowCount; i++)
                {
                    dtgv_Sanpham.Rows[i].Cells[0].Value = i + 1;
                }
            }
            catch { }
        }

        private void dtgv_Sanpham_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                int dong = e.RowIndex;
                this.txtmasp.Text = dtgv_Sanpham.Rows[dong].Cells[1].Value.ToString();
                this.txttensp.Text = dtgv_Sanpham.Rows[dong].Cells[2].Value.ToString();
                this.cbomaloai.Text = dtgv_Sanpham.Rows[dong].Cells[3].Value.ToString();
                this.cbonsx.Text = dtgv_Sanpham.Rows[dong].Cells[4].Value.ToString();
                this.txtdvt.Text = dtgv_Sanpham.Rows[dong].Cells[5].Value.ToString();
                this.txtgiamua.Text = dtgv_Sanpham.Rows[dong].Cells[6].Value.ToString();
                this.txtgiaban.Text = dtgv_Sanpham.Rows[dong].Cells[7].Value.ToString();
                this.txtsoluong.Text = dtgv_Sanpham.Rows[dong].Cells[8].Value.ToString();
            }
            catch { }
        }

        private void btnsua_Click(object sender, EventArgs e)
        {
            Xulybutton(false);
            ts = false;
            txtmasp.Enabled = false;
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (txtmasp.TextLength == 0)
            {
                lbthongbao.Text = "Hãy nhập vào mã sản phẩm ...";
                txtmasp.Focus();
            }
            else
            {
                if (txttensp.TextLength == 0)
                {
                    lbthongbao.Text = "Hãy nhập vào tên sản phẩm";
                    txttensp.Focus();
                }
                else
                {
                    if (ts)
                    {
                        if (!ktra)
                        {
                            double gm = Convert.ToDouble(txtgiamua.Text);
                            double gb = Convert.ToDouble(txtgiaban.Text);
                            int sl = Convert.ToInt32(txtsoluong.Text);
                            sp.ThemSP(txtmasp.Text, txttensp.Text, cbomaloai.Text, cbonsx.Text, txtdvt.Text, gm, gb, sl);
                            frSanpham_Load(sender, e);
                            lbthongbao.Text = "";
                        }
                        else {
                            int slg = Convert.ToInt32(txtsoluong.Text);
                            MessageBox.Show("Mã sản phẩm đã tồn tại !!!");
                        }
                    }
                    else {                        
                        double gm = Convert.ToDouble(txtgiamua.Text);
                        double gb = Convert.ToDouble(txtgiaban.Text);
                        int sl = Convert.ToInt32(txtsoluong.Text);
                        sp.SuaSP(txtmasp.Text, txttensp.Text, cbomaloai.Text, cbonsx.Text, txtdvt.Text, gm, gb, sl);
                        frSanpham_Load(sender, e);
                        Xulybutton(true);
                        lbthongbao.Text = "";
                    }
                }
            }           
        }

        private void btnhuy_Click(object sender, EventArgs e)
        {
            Xulybutton(true);
        }

        private void cbo_tkmasp_TextChanged(object sender, EventArgs e)
        {
            dtgv_Sanpham.DataSource = sp.Timkiem_Masp(cbo_tkmasp.Text);
        }

        private void cbo_tkmasp_Click(object sender, EventArgs e)
        {
            cbo_tkmasp.DataSource = sp.ShowSP();
            cbo_tkmasp.ValueMember = "MASP";
            cbo_tkmasp.DisplayMember = "MASP";
        }

        private void txt_tktensp_TextChanged(object sender, EventArgs e)
        {
            dtgv_Sanpham.DataSource = sp.Timkiem_tenSP(txt_tktensp.Text);
        }

        private void cbo_tkmaloai_TextChanged(object sender, EventArgs e)
        {
            dtgv_Sanpham.DataSource = sp.Timkiem_ML(cbo_tkmaloai.Text);
        }

        private void cbo_tknsx_TextChanged(object sender, EventArgs e)
        {
            dtgv_Sanpham.DataSource = sp.Timkiem_NSX(cbo_tknsx.Text);
        }

        private void cbo_tkmaloai_Click(object sender, EventArgs e)
        {
            cbo_tkmaloai.DataSource = sp.ShowLSP();
            cbo_tkmaloai.ValueMember = "MALOAI";
            cbo_tkmaloai.DisplayMember = "MALOAI";
        }

        private void cbo_tknsx_Click(object sender, EventArgs e)
        {
            cbo_tknsx.DataSource = sp.ShowNSX();
            cbo_tknsx.ValueMember = "MANSX";
            cbo_tknsx.DisplayMember = "MANSX";
        }

        private void btntk_Click(object sender, EventArgs e)
        {
            double gia1, gia2;
            gia1 = Convert.ToDouble(cbo_tkgiatu.Text.ToString());
            gia2 = Convert.ToDouble(cbo_tkdengia.Text.ToString());
            dtgv_Sanpham.DataSource = sp.Timkiem(txt_tktensp.Text,cbo_tkmaloai.Text,cbo_tknsx.Text,gia1,gia2);
        }

        private void cbomaloai_Click(object sender, EventArgs e)
        {
            cbomaloai.DataSource = sp.ShowLSP();
            cbomaloai.ValueMember = "MALOAI";
            cbomaloai.DisplayMember = "MALOAI";
        }

        private void cbonsx_Click(object sender, EventArgs e)
        {
            cbonsx.DataSource = sp.ShowNSX();
            cbonsx.ValueMember = "MANSX";
            cbonsx.DisplayMember = "MANSX";            
        }

        private void btnxoa_Click(object sender, EventArgs e)
        {
            if (btnsua.Enabled)
            {
                DialogResult kq = MessageBox.Show("Bạn có chắc muốn xóa thài khoản " + txttensp.Text + "  ? ", "Thông báo", MessageBoxButtons.YesNo);
                if (kq == DialogResult.Yes)
                {

                    sp.XoaSP(txtmasp.Text);
                    frSanpham_Load(sender, e);
                }
            }
            else
            {
                MessageBox.Show("Hãy thực hiện lưu dữ liệu !!!");
            }
        }

        private void btnthem_Click(object sender, EventArgs e)
        {
            Xulybutton(false);
            txtmasp.Enabled = true;
            ts = true;
            txtmasp.Text = "sp"+(dtgv_Sanpham.RowCount + 1).ToString();
            txttensp.Clear();
            cbomaloai.Text = "";
            cbonsx.Text = "";
            txtdvt.Clear();
            txtgiamua.Text = "0";
            txtgiaban.Text = "0";
            txtsoluong.Text = "0";
        }

        private void txtmasp_TextChanged(object sender, EventArgs e)
        {
            ktra = false;
            foreach (DataRow row in sp.ShowSP().Rows)
            {
                if (row["MASP"].ToString().Equals(txtmasp.Text))
                {
                    ktra = true;
                }
            }
        }

        private void Check_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
                e.Handled = true;
        }

        private void btnthoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
